package com.qingluan.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Personal_Info_database extends SQLiteOpenHelper {
	public static final String PERSON_PICTURE = "picture_url";
	public static final String PERSON_NAME ="name";
	public static final String PERSON_TAG = "tag";
	public static final String PERSON_DESCRIBE = "describe";
	public static final String PERSON_PASSWORD = "password";
	public static final String PERSON_UID_PHONE = "phone";
	
	private static  String DB_NAME = "personal.db";
	public static final String TBL_NAME_MISSION = "my_info";
	private static final String ctreatDBmis = "create table " +
			TBL_NAME_MISSION + 
			"  ( uid text primary key  " +
			",name varchar(50) unique not null" +
			",tag text" +
			",picture_url text  " +
			",email text unique not null" +
			",describe text "+
			",phone text  " +
			",password text ) ";
	
	private SQLiteDatabase db;
	
	public Personal_Info_database(Context context) {
		super(context, Personal_Info_database.DB_NAME,null,1);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		this.db.execSQL(ctreatDBmis);
		
	}

	
	public void insertMyTag (ContentValues values){
		SQLiteDatabase db = getWritableDatabase();
		long res = db.insert(Personal_Info_database.TBL_NAME_MISSION, null
				, values);
		db.close();
		Log.d("I_GET","insert db line :"+String.valueOf(res));
		if (db != null){
			db.close();
		}
	}
	
	public Cursor query(String tableName){
		db = getWritableDatabase();
		Cursor c  = db.query(tableName, null, null, null
				, null, null, null, null);
		
		return c;
	}
	
	public void del(String tableName,String uid){
		if (db == null){
			db = getWritableDatabase();
			
		}
		db.delete(tableName, "uid=?"
				, new String[] {uid});
		if (db != null){
			db.close();
		}
	}
	
	public void close(){
		if (db != null){
			db.close();
		}
	}
	
	
	
	
	
	
	/*
	 * for query kinds of info
	 * 
	 */
	protected  String search(String user,String type){
		Cursor c = this.query(Personal_Info_database.TBL_NAME_MISSION);
		c.moveToFirst();
		if(c.getCount() ==0){
			Log.d("CACHE","no items");
			SQLiteDatabase db = getWritableDatabase();
			
			c.close();
			db.close();
			return null;
		}
		if (c.getString(c.getColumnIndex("uid")).equals(user)){
			String value = c.getString(c.getColumnIndex(type));
			SQLiteDatabase db = getWritableDatabase();
			
			if (value != null){
				c.close();
				db.close();
				return value;
			}
		}
		
		while (c.moveToNext()){
			String value = null;
			if (c.getString(c.getColumnIndex("uid")).equals(user)){
				value = c.getString(c.getColumnIndex(type));
				Log.d("database_work","found in database :"+value);
				c.close();
				SQLiteDatabase db = getWritableDatabase();
				db.close();
				if (value != null ){
					return value;
				}
			}
		}
		
		c.close();
		SQLiteDatabase db = getWritableDatabase();
		db.close();
		return null;
		
	}
	
	/*
	 * help for update moudle or other moulde
	 */
	/*
	public int getUId(String user){
		Cursor c = this.query(TBL_NAME_MISSION);
		c.moveToFirst();
		int id = 0;
		while (c.moveToNext()){
			if (c.getString(c.getColumnIndex("name")).equals(user)){
				id = c.getInt(c.getColumnIndex("uid"));
				c.close();
				return id;
			}
		}
		//if (c.isLast()){
		//	c.close();
		//}
		return -1;
	}
	/*
	 * update info on table
	 */
	public void update(ContentValues values ,String where){
		
		SQLiteDatabase db = getWritableDatabase();
		db.update(TBL_NAME_MISSION, values, "uid="+where, null);
		db.close();
		
	}
	
	
	public String searchBitmap(String user){
		String uri = null;
		uri = this.search(user,"picture_url");
		SQLiteDatabase db = getWritableDatabase();
		db.close();
		return uri;
	}
	
	public String searchName(String user){
		String name = null;
		name = this.search(user,"name");
		SQLiteDatabase db = getWritableDatabase();
		db.close();
		return name;
	}
	
	public String searchTag(String user){
		String tag = null;
		tag = this.search(user,"tag");
		SQLiteDatabase db = getWritableDatabase();
		db.close();
		return tag;
	}
	
	public String searchEmail(String user){
		String email = null;
		email = this.search(user, "email");
		SQLiteDatabase db = getWritableDatabase();
		db.close();
		
		return email;
		
	}
	
	public String searchPhone(String user){
		String phone = null;
		phone  = this.search(user, "uid");
		SQLiteDatabase db = getWritableDatabase();
		db.close();
		
		return phone;
	}
	
	public String searchPassword(String user){
		String pass = null;
		pass  = this.search(user, "password");
		SQLiteDatabase db = getWritableDatabase();
		db.close();
		
		return pass;
	}
	
	public String getUserByEmail(String email){
		String uid = null;
		Cursor c = this.query(TBL_NAME_MISSION);
		if(c.isLast()){
			c.close();
			
		}
		
		if (c.moveToFirst()){
			if (c.getString(c.getColumnIndex("email")).equals(email)){
				String first_uid = c.getString(c.getColumnIndex("uid"));
				c.close();
				SQLiteDatabase db = getWritableDatabase();
				db.close();

				return first_uid;
			}
		}
		while (c.moveToNext()){
			if (c.getString(c.getColumnIndex("email")).equals(email)){
				uid = c.getString(c.getColumnIndex("uid"));
				c.close();
				SQLiteDatabase db = getWritableDatabase();
				db.close();

				return uid;
			}
		}
		SQLiteDatabase db = getWritableDatabase();
		db.close();
		
		return null;
		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion
			, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	public boolean cloase(){
		if (this.db != null){
			this.db.close();
			return true;
		}
		return false;
	}
}
